package server;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Auction {

	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
			"dd.MM.yyyy HH:mm z");

	private static int ID_COUNTER = 1;

	private int id;
	private String description;
	private User owner;
	private Calendar deadline;
	private int highestBid;
	private User highestBidder;

	private Object idLock = new Object();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public Calendar getDeadline() {
		return deadline;
	}

	public void setDeadline(Calendar deadline) {
		this.deadline = deadline;
	}

	public int getHighestBid() {
		return highestBid;
	}

	public void setHighestBid(int highestBid) {
		this.highestBid = highestBid;
	}

	public User getHighestBidder() {
		return highestBidder;
	}

	public void setHighestBidder(User highestBidder) {
		this.highestBidder = highestBidder;
	}

	public Auction(User owner, String description, Calendar deadline) {
		synchronized (idLock) {
			this.id = ID_COUNTER;
			ID_COUNTER++;
		}
		this.owner = owner;
		this.deadline = deadline;
		this.description = description;
		this.highestBid = 0;
		this.highestBidder = null;
	}

	public String getDeadlineAsString() {
		return DATE_FORMAT.format(deadline.getTime());
	}

	@Override
	public String toString() {

		return id + ". '" + ((description != null) ? description : "none")
				+ "' " + ((owner != null) ? owner.getName() : "none") + " "
				+ DATE_FORMAT.format(deadline.getTime()) + " "
				+ (highestBid / 100.0) + " "
				+ ((highestBidder != null) ? highestBidder.getName() : "none");
	}
}
